package com.gmstu.springorm.common.utils;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * @author John.zhang
 * @version Id: GenericsUtils, v 1.0 2020/9/2 17:52 ZhiYong Exp $
 */

@Slf4j
public class GenericsUtils {


    public static Class getSuperClassGenericsType(Class clazz, int index) {
        Type genType = clazz.getGenericSuperclass();
        if (!(genType instanceof ParameterizedType)) {
            log.error(clazz.getSimpleName() + "'s superclass not " + ParameterizedType.class.getSimpleName());
            return Object.class;
        }
        Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
        if (params.length <= index || index < 0) {
            log.warn("Index" + index + ", Size of " + clazz.getSimpleName() + "'s " + ParameterizedType.class.getSimpleName() + ":" + params.length);
            return Object.class;
        }
        if (!(params[index] instanceof Class)) {
            log.warn(clazz.getSimpleName() + " not set the actual class on superclass generic parameter");
            return Object.class;
        }
        return (Class) params[index];
    }


}
